home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / perl / doc / parr.orig < prev    next >
Encoding:
Text File  |  1991-07-22  |  3.2 KB  |  159 lines

  1. #!/usr/bin/perl
  2.  
  3. # @(#)@ parr 4.1.6
  4.  
  5. # rearrange conforming PS code to print the pages in an arbitrary
  6. # order.  The -o option takes a list of ranges, like this:
  7. #    1-5    1-10,11-20    11-,1-10
  8. # usage: parr [-a4] [-o list] [file]
  9. #
  10. # jgreely@cis.ohio-state.edu, 89/10/23
  11. # modified by jv@mh.nl, 91/078/15
  12.  
  13. $order='';
  14. $signFlag='';
  15. $signCount=0;
  16. $DEBUG=0;
  17. $TMPDIR='/usr/tmp';
  18.  
  19. while ($_ = $ARGV[0],/^-/) {
  20.     shift;
  21.     last if /^-\-$/;
  22.     /^-o/ && ($order = shift,next);
  23.     /^-a4/ && ($a4flag++,next);
  24.     die "usage: parr [-a4] [-o list] [file]\n";
  25. }
  26.  
  27. $file = "$TMPDIR/p$$.header";
  28. @files = ($file);
  29. $sheet=0;
  30. open(FILE,">$file") || die "$file: $!\n";
  31. while (<>) {
  32.     #
  33.     # hack to use NeXT Preview: strip old '%%Pages:' lines
  34.     #
  35.     next if /^%%Pages:/;
  36.     if (/^%%Page:/) {
  37.         close(FILE);
  38.         $sheet++;
  39.         $file = "$TMPDIR/p$$.$sheet";
  40.         push(@files,$file);
  41.         open(FILE,">$file") || die "$file: $!\n";
  42.     }
  43.     if (/^%%Trailer/) {
  44.         close(FILE);
  45.         $file = "$TMPDIR/p$$.trailer";
  46.         push(@files,$file);
  47.         open(FILE,">$file") || die "$file: $!\n";
  48.     }
  49.     if (/^TeXDict begin @(a4|letter)/) {
  50.         # Insert twoup before switching to TeXDict
  51.         &twoup;
  52.         $twoup++;
  53.         print FILE "TeXDict begin @landscape\n";
  54.         next;
  55.     }
  56.     print FILE $_;
  57. }
  58. close(FILE);
  59. die "twoup insertion error\n" unless $twoup == 1;
  60.  
  61. @order = ();
  62. if ($order) {
  63.     foreach $range (split(/,/,$order)) {
  64.         ($start,$sep,$end) = split(/(-)/,$range);
  65.         $start = 1 unless $start;
  66.         $end = $sheet unless $end;
  67.         if ($sep) {
  68.             push(@order,$start..$end);
  69.         }else{
  70.             push(@order,$start);
  71.         }
  72.     }
  73. }elsif ($signFlag) {
  74.     if (! $signCount) {
  75.         $signCount = $sheet;
  76.         $signCount += (4 - $sheet % 4) if ($sheet % 4);
  77.     }else{
  78.         $signCount *=4;
  79.     }
  80.     for($base=0;$base<$sheet;$base+=$signCount) {
  81.         @tmp = ($signCount/2+$base);
  82.         push(@tmp,$tmp[0]+1,$tmp[0]+2,$tmp[0]-1);
  83.         while ($tmp[3] > $base) {
  84.             push(@order,@tmp);
  85.             @tmp = ($tmp[0]-2,$tmp[1]+2,$tmp[2]+2,$tmp[3]-2);
  86.         }
  87.     }
  88. }else{
  89.     @order = (1..$sheet);
  90. }
  91.  
  92. @tmp=@order;
  93. @order=();
  94. foreach $page (@tmp) {
  95.     push(@order,$page > $sheet ? "B" : $page);
  96. }
  97.  
  98. open(FILE,"$TMPDIR/p$$.header");
  99. $_ = <FILE>;
  100. print $_,"%%Pages: (atend)\n";
  101. print while <FILE>;
  102. close(FILE);
  103.  
  104. foreach $page (@order) {
  105.     $count++;
  106.     print "%%Page: ? $count\n%%OldPage: $page\n";
  107.     if ($page eq "B") {
  108.         print "showpage\n";
  109.     }else{
  110.         open(FILE,"$TMPDIR/p$$.$page");
  111.         while (<FILE>) {
  112.             print unless /^%%Page:/;
  113.         }
  114.         close(FILE);
  115.     }
  116. }
  117. open(FILE,"$TMPDIR/p$$.trailer");
  118. print while <FILE>;
  119. close(FILE);
  120. print "%%Pages: $count\n";
  121.  
  122. unlink @files unless $DEBUG;
  123. exit(0);
  124.  
  125. sub twoup {
  126. $factor = 0.707106781187;
  127.  
  128. # Measurements are in 1/100 inch approx.
  129. if ( $a4flag) {
  130.     $topmargin = -57;
  131.     $leftmargin = 28;
  132.     $othermargin = 445;    # do not change -- relative to $leftmargin
  133. }
  134. else {
  135.     $topmargin = -57;
  136.     $leftmargin = 60;
  137.     $othermargin = 445;    # do not change -- relative to $leftmargin
  138. }
  139. $vsize = -1 - $factor;
  140. print FILE <<EOD;
  141. /isoddpage true def
  142. /orig-showpage /showpage load def
  143. /showpage {
  144.         isoddpage not { orig-showpage } if
  145.         /isoddpage isoddpage not store 
  146.     } def
  147.  
  148. /bop-hook {
  149.     /vsize $vsize def
  150.         isoddpage 
  151.     { $factor $factor scale $topmargin $leftmargin translate }
  152.         { 0 $othermargin translate}
  153.     ifelse
  154.     } def
  155.  
  156. /end-hook{ isoddpage not { orig-showpage } if } def
  157. EOD
  158. }
  159.